package edu.jd.movieplatform.security.m_home;

import edu.jd.movieplatform.common.*;
import org.apache.tomcat.util.json.JSONParserTokenManager;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

@RestController
@RequestMapping("/security/m_home")
public class MHomeAPI {

    @Resource
    private CommonService commonService;

    @Resource
    private MHomeService mHomeService;

    //根据当前管理员获取菜单
    @GetMapping("/menus")
    public Result menus(@RequestHeader("Token") String token){
        CurrentManager currentManager = MTokenUtils.getUserInfo(token,commonService);

        List<MenuVO> menuVOList = mHomeService.getMenus(currentManager);

        return Result.success(menuVOList);
    }

    @GetMapping("/currentManager")
    public Result currentManager(@RequestHeader("Token") String token){
        CurrentManager currentManager = MTokenUtils.getUserInfo(token, commonService);
        return Result.success(currentManager);
    }

    /**
     * 退出系统
     * @param token
     * @return
     */
    @DeleteMapping("/exit")
    public Result exit(@RequestHeader("Token") String token){
        //在服务端清除缓存的token
        MTokenUtils.removeToken(token);
        return Result.success();
    }

    /**
     * 下载文件
     * @param filename
     * @param response
     */
    @GetMapping("/avatar/{filename}")
    public void getAvator(@PathVariable String filename, HttpServletResponse response) throws IOException {

        //读取文件的输入流
        InputStream in = new FileInputStream(CurrentManager.AVATAR_DIR+"/"+filename);
        //将文件信息输出到浏览器的输出流
        OutputStream out = response.getOutputStream();

        byte[] b = new byte[10240];//容量为10240字节的缓存区
        int len = -1;//从输入流读取并放入缓存区的数据量

        while((len=in.read(b))!=-1){
            out.write(b,0,len);
        }
        out.flush();
        out.close();
        in.close();
    }

    @PostMapping("/avatar")
    public Result uploadAvaor( MultipartFile avatar,@RequestHeader("Token") String token) throws IOException {

        InputStream in = avatar.getInputStream();

        String name = UUID.randomUUID().toString();//获得通过uuid算法得到的不重复的字符串
        String submitedFilename = avatar.getOriginalFilename();//原始文件名
        String suffix = submitedFilename.substring(submitedFilename.lastIndexOf("."));
        String filename = name+suffix;
        OutputStream out = new FileOutputStream(CurrentManager.AVATAR_DIR+"/"+filename);

        byte[] b = new byte[10240];//容量为10240字节的缓存区
        int len = -1;//从输入流读取并放入缓存区的数据量

        while((len=in.read(b))!=-1){
            out.write(b,0,len);
        }
        out.flush();
        out.close();
        in.close();

        CurrentManager currentManager = MTokenUtils.getUserInfo(token,commonService);
        mHomeService.setAvatar(currentManager,filename);

        return Result.success();
    }

}
